import { Component, OnInit , ViewChild ,TemplateRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzButtonModule, NzButtonComponent  } from 'ng-zorro-antd/button';
import { NzFormatEmitEvent  } from 'ng-zorro-antd/core';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { FormBuilder, FormControl, FormGroup  } from '@angular/forms';
import { DelonFormModule,SFSchema , SFComponent , WidgetRegistry } from '@delon/form';
import { LbAAC001WidgetComponent } from '@shared';
import { HttpService, SupDic } from 'lbf';
import { Subject } from 'rxjs';
@Component({
  selector: 'app-xtgl-dept',
  templateUrl: './dept.component.html',
  styleUrls:['./dept.less']
})


export class XtglDeptComponent implements OnInit {

 
  editSchema: SFSchema = {
    properties: {
      DEPTID: {
        type: 'string',
        title: '结构编码',
        readOnly:true,
        ui:{
          grid:{
            span:8
          }
        }
      },
      DEPTNAME: {
        type: 'string',
        title: '机构名称',
        ui:{
          grid:{
            span:16
          }
        }
      },
      FDEPTID: {
        type: 'string',
        title: '上级机构',
        enum:this.supDic.getSFDic('DEPT'),
        readOnly:true,
        ui:{
          widget:"select",
          grid:{
            span:8
          }
        }
      },
      DEPTTYPE: {
        type: 'string',
        title: '机构属性',
        enum:this.supDic.getSFDic('JGLB'),
        ui:{
          widget:"select",
          grid:{
            span:8
          }
        }
      },
      SEQ: {
        type: 'number',
        title: '排列顺序',
        ui:{
          grid:{
            span:8
          }
        }
      },
      GRSY: {
        type: 'string',
        title: '自定义控件',
        
        ui:{
          widget:"lbGrsy",
          grid:{
            span:16
          }
        }
      } 
    },
    required:[ "DEPTNAME" ,"SEQ" ,"DEPTTYPE"],
    ui:{
      spanLabelFixed:100,
      grid:{
        gutter:20,
        span:8
      }
    }
  };

  addSchema:SFSchema = {
    properties: {
      FDEPTID: {
        type: 'string',
        title: '上级机构',
        enum:this.supDic.getSFDic('DEPT'),
        readOnly:true,
        ui:{
          widget:"select",
          grid:{
            span:24
          }
        }
      },
      DEPTNAME: {
        type: 'string',
        title: '机构名称',
        ui:{
          grid:{
            span:24
          }
        }
      },
      
      DEPTTYPE: {
        type: 'string',
        title: '机构属性',
        enum:this.supDic.getSFDic('JGLB'),
        ui:{
          widget:"select",
          grid:{
            span:12
          }
        }
      },
      SEQ: {
        type: 'number',
        title: '排列顺序',
        ui:{
          grid:{
            span:12
          }
        }
      },
      
    },
    required:[ "DEPTNAME" ,"SEQ" ,"DEPTTYPE"],
    ui:{
      spanLabelFixed:80,
      grid:{
        gutter:20,
        span:12
      }
    }
  };

  searchValue = '';
  gutter = 24;
  nodes:any = [];
  treeData:any;
  isSaveDisabled = true;
  isAddDisabled = true;
  isDelDisabled = true;
  formData ={};
  isVisible = false;
  modalTitle = '创建下级机构';

  @ViewChild('sf',{static:false}) sf: SFComponent;
  @ViewChild('addSf',{static:false}) addSf: SFComponent;
  @ViewChild('btnSave',{static:false}) btnSave: NzButtonComponent;
  
  constructor(private modal:ModalHelper,private lbs:HttpService ,private supDic:SupDic ,public msgSrv: NzMessageService ,widgetRegistry: WidgetRegistry) {
    widgetRegistry.register(LbAAC001WidgetComponent.KEY, LbAAC001WidgetComponent );
   }
  
  // 初始化机构树
  initDeptTree(){
     this.lbs.lbservice('qxgl_deptlist',{}).then(res=>{
       console.log(res.message.list);
       this.nodes = res.message.list;
     });
  }

  // 功能树单击事件
  nzEvent(event: NzFormatEmitEvent): void {
    this.treeData = event.node.origin;
    // console.log(this.addSf);
    // console.log(event);
    
    // 编辑框表单赋值
    this.sf.setValue('/DEPTID',this.treeData.key);
    this.sf.setValue('/DEPTNAME',this.treeData.title);
    this.sf.setValue('/FDEPTID',this.treeData.fdept);
    this.sf.setValue('/SEQ',this.treeData.seq);
    this.sf.setValue('/DEPTTYPE',this.treeData.depttype);
    // console.log(this.sf.getValue('/SEQ'));
    this.isSaveDisabled = true;
    this.isAddDisabled = false;
    this.isDelDisabled = false;
    
  }

  formChanged(event:NzFormatEmitEvent){
      this.isSaveDisabled = false;  
     
  }

  add(){
    this.formData = { FDEPTID:this.treeData.key }
    this.isVisible = true;
  }

  save(){
    if(!this.sf.valid)
    {
        this.msgSrv.error('必填项不能为空！');
        return;
    }
    this.lbs.lbservice('JGGL_MODI',{para:this.sf.value}).then(resdata=>{
      console.log(this.sf.value);
       if(resdata.code<1)
       {
         this.msgSrv.error(resdata.errmsg);
       }
       else
       {
        this.isSaveDisabled = true;
        this.initDeptTree();
        this.msgSrv.success('修改成功');
       }

    })
    
  }

  del(){

  }

  handleCancel(){
    this.isVisible = false;
    this.formData = {};
  }

  saveAdd(data :any)
  {
     
  }

  ngOnInit() { 
    this.initDeptTree();
  }
  
}
